<template>
  <view class="choose">
    <anil-seat
      v-if="anilSeatData.length"
      title="罗兰数字音乐教育石家庄校区"
      info="怀特艺术中心-国际庄大剧院"
      room-name="一场和青春有关的音乐会"
      :seatData="anilSeatData"
      :max="
        routerParams.buyNum === 'null' || routerParams.buyNum === null
          ? 1000
          : +routerParams.buyNum
      "
      @confirm="seatConfirm"
    >
    </anil-seat>
  </view>
</template>
<script>
import anilSeat from "@/uni_modules/anil-seat/components/anil-seat/anil-seat.vue";
// import {uniForms} from '@dcloudio/uni-ui'
import { seatData } from "@/utils/seat-data.js";
// console.log('seatData,', seatData);
export default {
  data() {
    return {
      anilSeatData: [],
      routerParams: {},
      positionList: {
        1: "左",
        2: "中",
        3: "右",
      },
    };
  },
  components: {
    anilSeat,
  },
  onLoad(option) {
    // this.anilSeatData = [
    //   {
    //     YCoord: "0", // 0 从左到右 第一排第一个的Y轴
    //     XCoord: "0", // 从左到右 第一排第一个的X轴
    //     SeatCode: "4413141101#01#14",
    //     Status: 2, // 座位状态
    //     RowNum: "1", // 排
    //     ColumnNum: "1", // 列
    //     Price: "99",
    //   },
    //   {
    //     YCoord: "0",
    //     XCoord: "2",
    //     SeatCode: "4413141101#01#13",
    //     Status: 0,
    //     RowNum: "1",
    //     ColumnNum: "2",
    //     Price: "99",
    //   },
    //   {
    //     YCoord: "0",
    //     XCoord: "3",
    //     SeatCode: "4413141101#01#12",
    //     Status: 0,
    //     RowNum: "1",
    //     ColumnNum: "3",
    //   },
    //   {
    //     YCoord: "1",
    //     XCoord: "0",
    //     SeatCode: "4413141101#01#11",
    //     Status: 0,
    //     RowNum: "2",
    //     ColumnNum: "1",
    //   },
    // ];
    this.routerParams = { ...option };
    // console.log(
    //   "null----",
    //   option.buyNum,
    //   option.buyNum === "null" || option.buyNum === null
    // );
    uni.request({
      url: "/roland-api/home/seats",
      data: {
        ...option,
      },
      dataType: "json",
      header: {
        Accept: "application/json",
        "Content-Type": "application/json; charset=utf-8",
      },
      success: (res) => {
        const resData = res.data;
        if (resData.code !== 0) {
          uni.showToast({
            title: resData.msg,
            duration: 3000,
            icon: "none",
          });
        } else {
          let data = res?.data?.data ?? [];
          let data1 = [
            {
              floor: 1,

              row: "2+",

              rowList: [
                {
                  id: 1,

                  seatNo: 1,

                  selected: false,
                },

                {
                  id: 2,

                  seatNo: 3,

                  selected: false,
                },

                {
                  id: 3,

                  seatNo: 5,

                  selected: false,
                },

                {
                  id: 4,

                  seatNo: 7,

                  selected: false,
                },

                {
                  id: 5,

                  seatNo: 9,

                  selected: false,
                },

                {
                  id: 6,

                  seatNo: 11,

                  selected: false,
                },

                {
                  id: 7,

                  seatNo: 13,

                  selected: false,
                },

                {
                  id: 8,

                  seatNo: 15,

                  selected: true,
                },

                {
                  id: 9,

                  seatNo: 17,

                  selected: true,
                },

                {
                  id: 10,

                  seatNo: 19,

                  selected: false,
                },

                {
                  id: 11,

                  seatNo: 21,

                  selected: false,
                },

                {
                  id: 12,

                  seatNo: 23,

                  selected: false,
                },

                {
                  id: 13,

                  seatNo: 25,

                  selected: false,
                },

                {
                  id: 14,

                  seatNo: 27,

                  selected: false,
                },

                {
                  id: 15,

                  seatNo: 29,

                  selected: false,
                },
              ],
            },

            {
              floor: 1,

              row: "1",

              rowList: [
                {
                  id: 16,

                  seatNo: 1,

                  selected: false,
                },

                {
                  id: 17,

                  seatNo: 3,

                  selected: false,
                },

                {
                  id: 18,

                  seatNo: 5,

                  selected: false,
                },

                {
                  id: 19,

                  seatNo: 7,

                  selected: true,
                },

                {
                  id: 20,

                  seatNo: 9,

                  selected: false,
                },

                {
                  id: 21,

                  seatNo: 11,

                  selected: true,
                },

                {
                  id: 22,

                  seatNo: 13,

                  selected: false,
                },

                {
                  id: 23,

                  seatNo: 15,

                  selected: true,
                },

                {
                  id: 23,

                  seatNo: 15,

                  selected: true,
                },

                {
                  id: 24,

                  seatNo: 17,

                  selected: false,
                },

                {
                  id: 25,

                  seatNo: 19,

                  selected: false,
                },

                {
                  id: 26,

                  seatNo: 21,

                  selected: false,
                },

                {
                  id: 27,

                  seatNo: 23,

                  selected: true,
                },

                {
                  id: 28,

                  seatNo: 25,

                  selected: false,
                },

                {
                  id: 29,

                  seatNo: 27,

                  selected: true,
                },

                {
                  id: 30,

                  seatNo: 29,

                  selected: false,
                },

                {
                  id: 31,

                  seatNo: 31,

                  selected: true,
                },
              ],
            },

            {
              floor: 1,

              row: "2",

              rowList: [
                {
                  id: 32,

                  seatNo: 1,

                  selected: false,
                },

                {
                  id: 33,

                  seatNo: 3,

                  selected: false,
                },

                {
                  id: 34,

                  seatNo: 5,

                  selected: false,
                },

                {
                  id: 35,

                  seatNo: 7,

                  selected: false,
                },

                {
                  id: 36,

                  seatNo: 9,

                  selected: false,
                },

                {
                  id: 37,

                  seatNo: 11,

                  selected: false,
                },

                {
                  id: 38,

                  seatNo: 13,

                  selected: false,
                },

                {
                  id: 39,

                  seatNo: 15,

                  selected: false,
                },

                {
                  id: 40,

                  seatNo: 17,

                  selected: false,
                },

                {
                  id: 41,

                  seatNo: 19,

                  selected: false,
                },

                {
                  id: 42,

                  seatNo: 21,

                  selected: false,
                },

                {
                  id: 43,

                  seatNo: 23,

                  selected: false,
                },

                {
                  id: 44,

                  seatNo: 25,

                  selected: false,
                },

                {
                  id: 45,

                  seatNo: 27,

                  selected: false,
                },

                {
                  id: 46,

                  seatNo: 29,

                  selected: false,
                },

                {
                  id: 47,

                  seatNo: 31,

                  selected: false,
                },

                {
                  id: 48,

                  seatNo: 33,

                  selected: false,
                },

                {
                  id: 49,

                  seatNo: 35,

                  selected: false,
                },
              ],
            },

            {
              floor: 1,

              row: "3",

              rowList: [
                {
                  id: 50,

                  seatNo: 1,

                  selected: false,
                },

                {
                  id: 51,

                  seatNo: 3,

                  selected: false,
                },

                {
                  id: 52,

                  seatNo: 5,

                  selected: false,
                },

                {
                  id: 53,

                  seatNo: 7,

                  selected: false,
                },

                {
                  id: 54,

                  seatNo: 9,

                  selected: false,
                },

                {
                  id: 55,

                  seatNo: 11,

                  selected: false,
                },

                {
                  id: 56,

                  seatNo: 13,

                  selected: false,
                },

                {
                  id: 57,

                  seatNo: 15,

                  selected: false,
                },

                {
                  id: 58,

                  seatNo: 17,

                  selected: false,
                },

                {
                  id: 59,

                  seatNo: 19,

                  selected: false,
                },

                {
                  id: 60,

                  seatNo: 21,

                  selected: false,
                },

                {
                  id: 61,

                  seatNo: 23,

                  selected: false,
                },

                {
                  id: 62,

                  seatNo: 25,

                  selected: false,
                },

                {
                  id: 63,

                  seatNo: 27,

                  selected: false,
                },

                {
                  id: 64,

                  seatNo: 29,

                  selected: false,
                },

                {
                  id: 65,

                  seatNo: 31,

                  selected: false,
                },

                {
                  id: 66,

                  seatNo: 33,

                  selected: false,
                },
              ],
            },
          ];
          // console.log("data: ", data);
          let newData = [];
          const fliterData = data.map((item, index, arr) => {
            // data = data.concat(item.rowList)
            // delete item.rowList
            // floor 楼层
            // position 1左， 2 中 3 右
            // row ： 排
            // seatNo 座位号
            // selected false 可选 true 不可选
            const newRowList = item.rowList.map((rowItem, subIdx) => {
              return {
                ...item,
                ...rowItem,
                Price: "99",
                YCoord: index.toString(), // 0 从左到右 第一排第一个的Y轴
                XCoord: (subIdx + 1).toString(), // 从左到右 第一排第一个的X轴
                SeatCode: rowItem.id,
                Status: Number(rowItem.selected), // 座位状态 -1为非座位，0为未购座位，1为已选座位(绿色),2为已购座位(红色)
                RowNum: `${item.row}`, // 排
                RowName: `${item.row}排`,
                ColumnNum: rowItem.seatNo, // 列
                ColumnName: `${rowItem.seatNo}列`, // 列
              };
            });
            return newRowList;
          });
          // console.log("fliterData", fliterData);
          newData = fliterData.reduce(function (a, b) {
            return a.concat(b);
          });
          this.anilSeatData = newData;
          // console.log("newData: ", newData);
          // console.table(newData, [
          //   "RowName",
          //   "ColumnName",
          //   "selected",
          //   "Status",
          // ]);
        }
      },
      fail: (err) => {
        console.log(err);
        uni.showToast({
          title: "抱歉，没有此类票信息!",
          icon: "none",
        });
        setTimeout(() => {
          uni.showLoading({
            title: "即将为您跳转购票/查询票据页面",
          });
        }, 1000);
        setTimeout(() => {
          uni.hideLoading();
          uni.navigateTo({
            url: `/`, //跳转地址
          });
        }, 2000);
      },
    });
  },
  methods: {
    // 打电话
    makePhone() {
      uni.makePhoneCall({
        phoneNumber: "18531937206", //仅为示例
        success: (res) => {
          console.log(" res,", res);
        },
        fail: (err) => {
          console.log("err,", err);
        },
      });
    },
    seatConfirm(itemList) {
      console.log("itemList: ", itemList);
      // customerId    客户id    必填
      // performanceTypeId  演出曲目id  必填
      // seatIds      座位id集合  必填  例：[1,2,3,4]
      const params = {
        customerId: this?.routerParams?.customerId ?? null,
        // performanceTypeId: this?.routerParams?.performanceTypeId ?? null,
        phone: this?.routerParams?.phone ?? null,
        seatIds: itemList?.map((item) => item.SeatCode) ?? null,
      };
      uni.request({
        url: "/roland-api/home/order",
        data: {
          ...params,
        },
        method: "POST",
        dataType: "json",
        header: {
          Accept: "application/json",
          "Content-Type": "application/json; charset=utf-8",
        },
        success: (res) => {
          const resData = res.data;
          if (resData.code !== 0) {
            uni.showToast({
              title: resData.msg,
              icon: "none",
              duration: 3000,
            });
            setTimeout(() => {
              uni.showLoading({
                title: "即将返回上一页面，请重新进入",
              });
              setTimeout(() => {
                uni.hideLoading();
                uni.navigateBack({
                  delta: 1
                });
              }, 2000);
            }, 3000);
          } else {
            let msg = '下单成功';
            if (resData.data > 0) {
              msg = '下单成功，请尽快联系老师进行线下付款～';
            }
            uni.showToast({
              title: msg,
              icon: "none",
              duration: 5000,
            });
            setTimeout(() => {
              uni.showLoading({
                title: "即将为您跳转购票/查询票据页面",
              });
              setTimeout(() => {
                uni.hideLoading();
                uni.navigateTo({
                  url: `/`, //跳转地址
                });
              }, 2000);
            }, 5000);
          }
        },
        fail: (err) => {
          console.log(err);
          uni.showToast({
            title: err,
            icon: "none",
          });
        },
      });
      console.log("params: ", params);
    },
  },
};
</script>

<style scoped lang="scss">
.choose {
  width: 100%;
  height: 100%;
}
</style>
